//
//  HomeViewController.m
//  OneCreditTreasure
//
//  Created by iOS Grouper on 16/8/26.
//
//

#import "HomeViewController.h"
#import "ScrollViewCollectionReusableView.h"//轮播头视图
#import "FourButtonCollectionViewCell.h"//四个选项
#import "SecondHandViewController.h"//二手车库
#import "DataViewController.h"//did分期购车
#import "RepayMoneyViewController.h"//did车贷还款
@interface HomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
    UICollectionView *myCollectionView;
}

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = LV;
    [self creatView];
}

- (void)creatView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //    layout.itemSize = CGSizeMake(100, 100);
    
    myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)collectionViewLayout:layout];
    [self.view addSubview:myCollectionView];
    myCollectionView.delegate = self;
    myCollectionView.dataSource = self;
    [myCollectionView registerClass:[FourButtonCollectionViewCell class] forCellWithReuseIdentifier:@"FourButtonCollectionViewCell"];
    [myCollectionView registerClass:[ScrollViewCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ScrollViewCollectionReusableView"];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FourButtonCollectionViewCell *fourCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FourButtonCollectionViewCell" forIndexPath:indexPath];
    return fourCell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            DataViewController *data = [DataViewController new];
            [self.navigationController pushViewController:data animated:YES];
        }
        if (indexPath.row == 3) {
            SecondHandViewController *secondHand=[SecondHandViewController new];
            [self.navigationController pushViewController:secondHand animated:YES];
        }
        if (indexPath.row == 1) {
            RepayMoneyViewController *repay = [RepayMoneyViewController new];
            [self.navigationController pushViewController:repay animated:YES];
        }
    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (section == 0) {
        return UIEdgeInsetsMake(30* AUTO_WIDTH, 30 * AUTO_HEIGHT , 50 * AUTO_WIDTH, 30 * AUTO_HEIGHT);
    }
    else{
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return CGSizeMake(70 * AUTO_WIDTH, 100 * AUTO_HEIGHT);
    }
    else{
        return CGSizeMake(kScreenW/2- 1, 70 * AUTO_HEIGHT);
    }
}
//上下时间距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    if (section == 1) {
        return 1;
    }
    else{
        return 10 * AUTO_HEIGHT;
    }
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    if (section == 1) {
        return 1;
    }
    else{
        return 10 * AUTO_WIDTH;
    }
}






- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 4;
    }
    if (section == 1) {
        return 4;
    }
    return 0;
}



- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        ScrollViewCollectionReusableView *head = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"ScrollViewCollectionReusableView" forIndexPath:indexPath];
        return head;
    }
    else{
        return nil;
    }
}
//头视图高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        
        return CGSizeMake(kScreenW, 150 * AUTO_HEIGHT);
    }
    else{
        return CGSizeMake(0 , 0 *AUTO_HEIGHT);
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
